package ncit.bluetooth.robots;

import ncit.bluetooth.connect.BluetoothChatService;
import ncit.bluetooth.settings.BluetoothControlSettings;
import android.util.Log;


public class GenericBluetoothDevice extends Robot {

	//DEBUG
	private static final String TAG = "bluetooth.robots.GenericBluetoothDevice";
	
	private String command;
	private String power;
	
	public GenericBluetoothDevice()
	{
		command = "";
		power = "";
	}
	
	
	@Override
	public void goForwardBackward(float slope) {
		// TODO Auto-generated method stub
		Log.i(TAG,"Entered goForwardBackward.");
		
		if(slope > 0)
			command = BluetoothControlSettings.goForwardByte;
		else if(slope < 0)
			command = BluetoothControlSettings.goBackwardsByte;
		else 
			command = "";
		
		int maxPower = 0;
		try{
			
			if(slope > 0)
				maxPower = Integer.parseInt(BluetoothControlSettings.maxPowerForward);
			else if(slope < 0)
				maxPower = Integer.parseInt(BluetoothControlSettings.maxPowerBackwards);
			else
				maxPower = 0;
			
		}catch(NumberFormatException e){
			
			Log.i(TAG, "Values introduced in the maximum power fields are invalid.");
			
		}
		
		int p = (int) Math.abs(slope * maxPower); 
		power = p + "";
		sendMessage();
	}

	@Override
	public void motorBreak() {
		// TODO Auto-generated method stub
		
		command = BluetoothControlSettings.goForwardByte;
		power = "0";
		
		
	}

	@Override
	public void startMotors() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void goLeftRight(float slope) {
		// TODO Auto-generated method stub
		
		if(slope > 0)
			command = BluetoothControlSettings.goRightByte;
		else if(slope < 0)
			command = BluetoothControlSettings.goLeftByte;
		else 
			command = "";
		
		int maxPower = 0;
		try{
			
			if(slope > 0)
				maxPower = Integer.parseInt(BluetoothControlSettings.maxPowerRight);
			else if(slope < 0)
				maxPower = Integer.parseInt(BluetoothControlSettings.maxPowerLeft);
			else
				maxPower = 0;
			
		}catch(NumberFormatException e){
			
			Log.i(TAG, "Values introduced in the maximum power fields are invalid.");
			
		}
		
		int p = (int) Math.abs(slope * maxPower); 
		power = p + "";
		sendMessage();
		
	}

	@Override
	public void askSensors() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startSensors() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void park() {
		// TODO Auto-generated method stub
		
	}

	private void sendMessage()
	{
		
		char[] powerChars = power.toCharArray();
		char[] commandChars = command.toCharArray();
		
		int powerCharsLength = powerChars.length;
		int commandCharsLength = commandChars.length; 
		for(int i = 0; i < commandCharsLength; i ++)
		{
			BluetoothChatService.getInstance().write((byte)commandChars[i]);
		}
		
		for(int i = 0; i < powerCharsLength; i ++)
		{
			BluetoothChatService.getInstance().write((byte)powerChars[i]);
		}
		
		
	}
	
	
}
